package com.ruoyi.report_lq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lq.domain.bo.ReportRhlqSssylBsInfoBo;
import com.ruoyi.report_lq.domain.vo.ReportRhlqSssylBsInfoVo;
import com.ruoyi.report_lq.domain.ReportRhlqSssylBsInfo;
import com.ruoyi.report_lq.mapper.ReportRhlqSssylBsInfoMapper;
import com.ruoyi.report_lq.service.IReportRhlqSssylBsInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author zyb
 * @date 2024-07-02
 */
@RequiredArgsConstructor
@Service
public class ReportRhlqSssylBsInfoServiceImpl implements IReportRhlqSssylBsInfoService {

    private final ReportRhlqSssylBsInfoMapper baseMapper;

    private final ReportRhlqSssylBaDetailServiceImpl detailService;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    @FIllBasicInfo
    public ReportRhlqSssylBsInfoVo queryById(Long excelId){
        ReportRhlqSssylBsInfoVo info = baseMapper.selectVoById(excelId);
        // TODO 查询子表的数据
        info.setBaList(detailService.queryByInfoId(info.getExcelId()));
        return info;
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<ReportRhlqSssylBsInfoVo> queryPageList(ReportRhlqSssylBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportRhlqSssylBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportRhlqSssylBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<ReportRhlqSssylBsInfoVo> queryList(ReportRhlqSssylBsInfoBo bo) {
        LambdaQueryWrapper<ReportRhlqSssylBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportRhlqSssylBsInfo> buildQueryWrapper(ReportRhlqSssylBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportRhlqSssylBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportRhlqSssylBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportRhlqSssylBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportRhlqSssylBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportRhlqSssylBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportRhlqSssylBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportRhlqSssylBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportRhlqSssylBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportRhlqSssylBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportRhlqSssylBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), ReportRhlqSssylBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportRhlqSssylBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportRhlqSssylBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportRhlqSssylBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportRhlqSssylBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportRhlqSssylBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportRhlqSssylBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportRhlqSssylBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportRhlqSssylBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getQuydd()), ReportRhlqSssylBsInfo::getQuydd, bo.getQuydd());
        lqw.eq(StringUtils.isNotBlank(bo.getJizlqcd()), ReportRhlqSssylBsInfo::getJizlqcd, bo.getJizlqcd());
        lqw.eq(StringUtils.isNotBlank(bo.getRuhlqpz()), ReportRhlqSssylBsInfo::getRuhlqpz, bo.getRuhlqpz());
        lqw.eq(StringUtils.isNotBlank(bo.getRuhlqdh()), ReportRhlqSssylBsInfo::getRuhlqdh, bo.getRuhlqdh());
        lqw.eq(StringUtils.isNotBlank(bo.getJizlqzl()), ReportRhlqSssylBsInfo::getJizlqzl, bo.getJizlqzl());
        lqw.eq(StringUtils.isNotBlank(bo.getJizlqbh()), ReportRhlqSssylBsInfo::getJizlqbh, bo.getJizlqbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShiybh()), ReportRhlqSssylBsInfo::getShiybh, bo.getShiybh());
        lqw.eq(StringUtils.isNotBlank(bo.getZhengfdjbsxflqwlqk()), ReportRhlqSssylBsInfo::getZhengfdjbsxflqwlqk, bo.getZhengfdjbsxflqwlqk());
        lqw.eq(StringUtils.isNotBlank(bo.getDianhcz()), ReportRhlqSssylBsInfo::getDianhcz, bo.getDianhcz());
        lqw.eq(StringUtils.isNotBlank(bo.getRuylzlx()), ReportRhlqSssylBsInfo::getRuylzlx, bo.getRuylzlx());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(ReportRhlqSssylBsInfoBo bo) {
        ReportRhlqSssylBsInfo add = BeanUtil.toBean(bo, ReportRhlqSssylBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(ReportRhlqSssylBsInfoBo bo) {
        boolean flag;
        ReportRhlqSssylBsInfo update = BeanUtil.toBean(bo, ReportRhlqSssylBsInfo.class);
        validEntityBeforeSave(update);
        flag = baseMapper.updateById(update) > 0;
        // TODO 更新或新增子表
        flag = detailService.insertOrUpdateBySylBo(bo.getBaList(), update.getExcelId());
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportRhlqSssylBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
